package com.CusterRelSys.control;

import com.CusterRelSys.entity.Page;
import com.CusterRelSys.entity.SalChance;
import com.CusterRelSys.entity.SysUser;
import com.CusterRelSys.service.ISalChanceZjjService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by 张俊杰 on 2017/12/1.
 */
@Controller
@RequestMapping("/salc")
@SessionAttributes({"salChancePage"})
public class SalChanceZjjControl {

    @Autowired
    ISalChanceZjjService salChanceZjjService;

    /**
     * 分页查询销售机会表
     * @param modelMap
     * @param pageNo 页号
     * @param pageSize 每页显示的行数
     * @param salChance
     * @return
     */
    @RequestMapping("/scPage")
    public String selectSalChancePage(ModelMap modelMap, Integer pageNo, Integer pageSize, SalChance salChance, HttpSession session){

        Page<SalChance> salChancePage = (Page<SalChance> )session.getAttribute("salChancePage");

        if (salChancePage == null || pageNo == null) {
            salChancePage = new Page<>();
            salChancePage.setQueryObject(salChance);
            salChancePage.setPageNo(1);
            int total = salChanceZjjService.countPage(salChancePage);
            salChancePage.setTotalRow(total);
            modelMap.addAttribute("salChancePage",salChancePage);
        }else {
            salChancePage.setPageNo(pageNo);
        }
        if(pageSize != null){
            salChancePage.setPageSize(pageSize);
        }
        List<SalChance> salChances = salChanceZjjService.selectSalChanceByPage(salChancePage);
        modelMap.addAttribute("salChances",salChances);
        return "forward:/Views/~sale/listZjj.jsp";
    }

    /**
     * 删除
     * @param salChance
     */
    @RequestMapping("/del")
    public @ResponseBody void deleteSalChances(SalChance salChance){
        salChanceZjjService.deleteSalChanceById(salChance);
    }

    /**
     * 查询用户所有记录
     * @return
     */
    @RequestMapping("/userall")
    public @ResponseBody List<SysUser> selectSysUserAll(){
        List<SysUser> sysUsers = salChanceZjjService.selectSysUserAll();
        return sysUsers;
    }

    /**
     * 添加
     * @param salChance
     * @param chc_DueDate
     * @param chc_DueTo
     * @param chc_CreateDate
     * @param chc_CreateBy
     * @return
     * @throws ParseException
     */
    @RequestMapping("/add")
    public String addSalChances(SalChance salChance,
                                String chc_DueDate,
                                String chc_DueTo,
                                String chc_CreateDate,
                                String chc_CreateBy) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        if(StringUtils.isNotEmpty(chc_CreateDate)){
            date = format.parse(chc_CreateDate);
            salChance.setChcCreateDate(date.getTime());
        }
        if(StringUtils.isNotEmpty(chc_DueDate )){
            date = format.parse(chc_DueDate);
            salChance.setChcDueDate(date.getTime());
        }
        if (StringUtils.isNotEmpty(chc_CreateBy)){
            String[] split = chc_CreateBy.split(",");
            salChance.setChcCreateId(Integer.parseInt(split[0]));
            salChance.setChcCreateBy(split[1]);
        }
        if (StringUtils.isNotEmpty(chc_DueTo)){
            String[] split = chc_DueTo.split(",");
            salChance.setChcDueId(Integer.parseInt(split[0]));
            salChance.setChcDueTo(split[1]);
        }
        System.out.println(salChance);
       boolean b = salChanceZjjService.insertSalChance(salChance);
        if(b){
            return "redirect:/Views/~sale/addZjj.jsp?flag=1";
        }else {
            return "redirect:/Views/~sale/addZjj.jsp?flag=0";

        }
       //return null;
    }

    /**
     * 单值查询返回到dispatchZjj界面
     * @param modelMap
     * @param chcId
     * @return
     */
    @RequestMapping("/dispatch")
    public String dispatch(ModelMap modelMap,Integer chcId){
        SalChance salChance = salChanceZjjService.selectSalChanceById(chcId);
        if(salChance != null){
            modelMap.addAttribute("salChance",salChance);
            return "forward:/Views/~sale/dispatchZjj.jsp";
        }else {
            return "forward:/salc/scPage";
        }
    }

    /**
     * 单值查询返回到editZjj界面
     * @param modelMap
     * @param chcId
     * @return
     */
    @RequestMapping("/edit")
    public String edit(ModelMap modelMap,Integer chcId){
        SalChance salChance = salChanceZjjService.selectSalChanceById(chcId);
        if(salChance != null){
            modelMap.addAttribute("salChance",salChance);
            return "forward:/Views/~sale/editZjj.jsp";
        }else {
            return "forward:/salc/scPage";
        }
    }

    /**
     * 指派销售机会
     * @param salChance
     * @param chc_DueDate
     * @return
     */
    @RequestMapping("/dispatchUpdate")
    public String dispatchUpdate(SalChance salChance,String chc_DueDate){
        boolean b = salChanceZjjService.updateSalChanceByIdDispatch(salChance, chc_DueDate);

        if(b){
            return "redirect:/salc/scPage";
        }else {
            return "redirect:/salc/dispatch?chcId="+salChance.getChcId();
        }

    }
    /**
     * 编辑销售机会
     * @param salChance
     * @param chc_DueDate
     * @return
     */
    @RequestMapping("/editUpdate")
    public String dispatchUpdate(SalChance salChance, String chc_DueDate, String chc_CreateDate){
        boolean b = salChanceZjjService.updateSalChanceByIdEdit(salChance, chc_DueDate,chc_CreateDate);
        if(b){
            return "redirect:/salc/scPage";
        }else {
            return "redirect:/salc/edit?chcId="+salChance.getChcId();
        }

    }

    public String changeString(String ecode){
        try {
            return new String(ecode.getBytes("iso-8859-1"),"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    @RequestMapping("/test")
    public void test(String abc){
        System.out.println(abc);
    }
}
